# -- coding: utf-8 --
# -- coding: utf-8 --
# -*- coding: UTF-8 -*-
"""
@author:wubailong
@time:2021/01/21
广州公共资源交易中心
search_dict = {
    "source_type": "广州公共资源交易中心",
    #"公告类型": "建设工程 ",
    #"工程类型": "中标结果公告",
    #"city": "广州",
}
# 文件名配置
cfgs = {
    "your_name": "QuinceyWu",
    "source_type": "gdggzyjypt",
    "type": "zb",
}
"""
import os
import re
import pymongo
from extractors.base_extractor import BaseExtractor
from spidertools.utils.xpath_utils import get_alltext, get_all_tables
from spidertools.utils.text_utils import replace_punctuation
from spidertools.utils.snippets import combine_two_dict
from info_fsm import InfoMachine
from table_info_extract.common_table_utils import common_table_extrator
from table_info_extract import dict_mapping_triedTree
from pprint import pprint
from table_info_extract.table_fsm import TableExtractMachine
from scrapy.selector import Selector
from utils.logger_utils import savelog, LogStateString


class MongoDb(object):

    def __init__(self):
        self.my_cli = pymongo.MongoClient("mongodb://127.0.0.1:27017")
        self.my_db = self.my_cli["construction"]
        self.my_col = self.my_db["project_html"]
        self.select_url = []

    @property
    def query_condition(self):
        # return {"source_type": "广东省全国公共资源交易平台", "公告类型": "招标公告"}
        return {
            "origin_url": "http://ggzy.gz.gov.cn/jyywjsgcjtgczbgg/514664.jhtml"}
    # 'http://ggzy.gz.gov.cn/jyywjsgcfwjzzbgg/614950.jhtml'
    # 'http://ggzy.gz.gov.cn/jyywjsgcszgczbgg/510496.jhtml'
    # 'http://ggzy.gz.gov.cn/jyywjsgcmhgczbgg/481354.jhtml'
    # 'http://ggzy.gz.gov.cn/jyywjsgcfwjzzbgg/623541.jhtml'
    # 'http://ggzy.gz.gov.cn/jyywjsgcfwjzzbgg/626180.jhtml'
    # 'http://ggzy.gz.gov.cn/jyywjsgcmhgczbgg/503920.jhtml'
    # 'http://ggzy.gz.gov.cn/jyywjsgcszgczbgg/585188.jhtml'
    # "http://ggzy.gz.gov.cn/jyywjsgcfwjzzbgg/624396.jhtml"
    # "http://ggzy.gz.gov.cn/jyywjsgcszgczbgg/580077.jhtml",
    # 'http://ggzy.gz.gov.cn/jyywjsgcfwjzzbgg/603680.jhtml'
    # def query_condition(self, url):
    #     return {"origin_url": f"{url}"}

    def get_db_info(self):
        # for url in self.select_url:
        #     for info in self.my_col.find(self.query_condition(url)):
        #         yield info
        for info in self.my_col.find(self.query_condition):
            yield info


class GuangZhouGongGongZiYuanJiaoYiZhongXin(BaseExtractor):
    def __init__(self, info_dict):
        super(GuangZhouGongGongZiYuanJiaoYiZhongXin, self).__init__(info_dict)

    def check_purchase(self):
        flag = 0
        if "补充" in self.info_dict['公告标题']:
            flag = 1
        elif "采购" in self.info_dict['公告标题']:
            flag = 1
        elif "暂停" in self.info_dict['公告标题']:
            flag = 1
        elif "变更" in self.info_dict['公告标题']:
            flag = 1
        elif "恢复" in self.info_dict['公告标题']:
            flag = 1
        elif "终止" in self.info_dict['公告标题']:
            flag = 1
        elif "修正" in self.info_dict['公告标题']:
            flag = 1
        elif "有效期" in self.info_dict['公告标题']:
            flag = 1
        elif '函' in self.info_dict['公告标题']:
            flag = 1
        return flag

    def zhaobiao_parse(self):
        self.html = self.html.replace("网址：", "不要").replace("（网址", "不要").replace("(盖章)", '不要')\
            .replace('投标人（盖公章）', '').replace('：', ':').replace("（地址", "不要")
        self.sel = Selector(text=self.html)
        content_root_nodes = self.sel.xpath('//div[@class="Section1"]/*')
        nodes = []
        for node in content_root_nodes:
            node_pu = str(node.xpath('string(.)')[0].extract()).replace("\xa0", '').replace(" ", "")
            if '附件一:' == node_pu:
                break
            elif '投标登记申请表（用于投标登记）' in node_pu:
                break
            elif '资格审查条件（资格最低条件）' in node_pu:
                break
            elif '公告附件一' in node_pu:
                break
            elif '招标公告附表' in node_pu:
                break
            elif '特此声明' in node_pu:
                break
            elif '标段的划分及主要工程项目情况' in node_pu:
                break
            elif '施工管理班子人员要求表' in node_pu:
                break
            elif '（单独递交,不用装订，1式2份）' in node_pu:
                break
            elif '投标人业绩证明' in node_pu:
                break
            elif '附件1投标登记申请表' in node_pu:
                break
            elif '--------------------' in node_pu:
                break
            elif '投标申请人报名提交资料一览表（本表仅为报名用）' in node_pu:
                break
            elif '招标人或招标代理机构接收资料人员签名：' in node_pu:
                break
            elif '资格审查条件（最低要求）' in node_pu:
                break
            elif '资格预审申请文件的组成' in node_pu:
                break
            elif '投标申请人（盖章）' in node_pu:
                break
            elif '附件1：' in node_pu:
                break
            elif '附录1资格审查条件(资质最低条件)' in node_pu:
                break
            else:
                nodes.append(node_pu)

        output_dict = self.texts_matchine_parse(nodes)

        html_info_dict = dict_mapping_triedTree(self.info_dict)
        output_dict = combine_two_dict(output_dict, html_info_dict)

        if '工程基本信息' not in output_dict.keys():
            output_dict['工程基本信息'] = {}
        output_dict['工程基本信息']['项目名称'] = str(output_dict['工程公告信息']['公告标题']).replace('招标公告', '')

        if '工程招标信息' not in output_dict.keys():
            output_dict['工程招标信息'] = {}
        output_dict['工程招标信息']['标段名称'] = output_dict['工程基本信息']['工程类别']

        return output_dict

    def zhongbiao_parse(self):
        self.sel = Selector(text=self.html)
        output_dict = {}
        tables = get_all_tables(self.sel)
        for table in tables:
            result = self.table_extract_machine.run_table(table)
            output_dict = combine_two_dict(output_dict, result)
            html_info_dict = dict_mapping_triedTree(self.info_dict)
            output_dict = combine_two_dict(output_dict, html_info_dict)
            if '工程招标信息' not in output_dict.keys():
                output_dict['工程招标信息'] = {}
            output_dict['工程招标信息']['标段名称'] = output_dict['工程基本信息']['工程类别']
        return output_dict

    def start_parse(self):

        if not self.checkIfNeedParse():
            savelog(LogStateString.Waring_NoValidConent, msg_dict=self.info_dict)
            return {"not_need_checkjson": '1'}

        if self.check_purchase():
            savelog(LogStateString.Waring_NoNeedParse, msg_dict={"url": self.info_dict["origin_url"]})
            return {"not_need_checkjson": '1'}

        result = self.parse_announcement_info()
        if not result:
            result = self.commom_announcement_parse()

        result = self.get_infos_from_otherway(result, self.html)

        result = self.clean_dict_keys(result)
        print(result)
        return result

    def parse_announcement_info(self):
        result = {}
        if '招标公告' in self.info_dict['公告类型']:
            result = self.zhaobiao_parse()
        elif '中标公告' in self.info_dict['公告类型']:
            result = self.zhongbiao_parse()
        return result


if __name__ == '__main__':
    m = MongoDb()
    for info in m.get_db_info():
        g = GuangZhouGongGongZiYuanJiaoYiZhongXin(info_dict=info)
        g.output_extractor_dict()
